#!/bin/bash

JAVAC="`which javac`"

if [ ! -x "$JAVAC" ]; then
	echo "Couldn't find Java Compiler..."
	exit 1
fi
$JAVAC -cp .:server:junit-4.1.jar controller/*.java
if [ ! $? == "0" ]; then
	echo "Error compiling client source code... please fix the errors"
	exit 1
fi
$JAVAC -cp .:server:junit-4.1.jar view/*.java
if [ ! $? == "0" ]; then
	rm controller/*.class
	echo "Error compiling client source code... please fix the errors"
	exit 1
fi
$JAVAC -cp .:server:junit-4.1.jar *.java
if [ ! $? == "0" ]; then
	rm controller/*.class
	rm view/*.class
	echo "Error compiling client source code... please fix the errors"
	exit 1
fi
$JAVAC -cp .:server:junit-4.1.jar server/*.java
if [ ! $? == "0" ]; then
	rm controller/*.class
	rm view/*.class
	echo "Error compiling client source code... please fix the errors"
	exit 1
fi
echo "Main-Class: Client" > Client.meta
jar cmf Client.meta client.jar view controller server Client.class
if [ ! $? == "0" ]; then
	rm *.class
	rm view/*.class
	rm controller/*.class
	rm Client.meta
	echo "Error creating client JAR file... please fix the errors"
	exit 1
fi
rm Client.meta
rm *.class
rm view/*.class
rm view/protocol/*.class
rm controller/*.class
rm server/*.class
echo "Done, client.jar created OK."
